package com.csw.controller;

import com.csw.entity.Dir;
import com.csw.entity.File;
import com.csw.entity.Share;
import com.csw.entity.User;
import com.csw.service.DirService;
import com.csw.service.FileService;
import com.csw.service.ShareService;
import com.csw.service.UserService;
import com.csw.util.ObjectIdUtil;
import com.csw.util.TextCopyFileAndMove;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("public")
@RestController
public class PublicController {
    @Autowired
    private ShareService shareService;
    @Autowired
    private FileService fileService;
    @Autowired
    private DirService dirService;
    @Autowired
    private UserService userService;

    @RequestMapping("queryAll")
    public String queryAll(HttpSession session) {

            User user = (User) session.getAttribute("user");
            if (user == null) {
                return "redirect:/user/logOut";
            }
            List<Share> shareListfiles = shareService.queryAllFiles();
            for (Share share : shareListfiles) {
                if (share.getFile() == null) {
                    shareService.deleteById(share.getId());
                }
            }
            shareListfiles = shareService.queryAllFiles();

            List<Share> shareListDirs = shareService.queryAllDirs();
            for (Share share : shareListDirs) {
                if (share.getDir() == null) {
                    shareService.deleteById(share.getId());
                }
            }
            shareListDirs = shareService.queryAllDirs();
            session.setAttribute("shareListfiles", shareListfiles);
            session.setAttribute("shareListDirs", shareListDirs);
            return "redirect:/jsp/front/main/detail/public.jsp";

    }

    @RequestMapping("getMessage")
    public String getMessage(HttpSession session) {

        String message = (String) session.getAttribute("message");
            return message;

    }

    /**
     * @param session
     * @return
     */
    @RequestMapping("cleMessage")
    public String cleMessage(HttpSession session) {

        session.setAttribute("message", null);
            return "redirect:/public/queryAll";

    }

    @RequestMapping("onsaves")
    public String onsaves(String fid, HttpSession session, String aa) throws Exception {

            User user = (User) session.getAttribute("user");
            if (fid != null) {
                session.setAttribute("onsaves", fid);
                session.setAttribute("message", "去选择保存路径");
                return "redirect:/mydoc/queryOne";
            } else {
                if (aa.equals("0")) {
                    session.setAttribute("onsaves", null);
                    return "redirect:/mydoc/queryOne";
                } else if (aa.equals("1")) {
                    fid = (String) session.getAttribute("onsaves");
                    Share share = shareService.queryById(fid);
                    File file = fileService.selectFileById(share.getFile().getId());
                    String folderid = (String) session.getAttribute("did");
                    Dir dir = dirService.selectById(folderid);
                    //保存文件
                    //服务器移动
                    String topath = dir.getPath() + "\\" + dir.getName();
                    java.io.File file1 = new java.io.File(file.getPath() + "\\" + file.getNewName());
                    String name = file.getNewName();
                    TextCopyFileAndMove.copyFileToDir(topath, file1, name);

                    //更新数据
                    file.setId(ObjectIdUtil.getObjectId());
                    file.setPath(topath);
                    file.setDir(dir);
                    file.setUser(user);
                    //md5
                    List<File> filem = fileService.selectFileByPidAndMd5(dir.getId(), file.getCheckMd5());
                    if (filem.size() != 0) {
                        //已有相同文件，不在进行操作"
                    } else {
                        //当前文件夹下没有相同的文件
                        fileService.addFile(file);
                        session.setAttribute("fileYdid", null);
                    }
                    //保存成功
                    session.setAttribute("onsaves", null);
                    session.setAttribute("message", "保存成功");
                    return "redirect:/mydoc/queryOne";
                }
            }
            return "redirect:/mydoc/queryOne";

    }

    @RequestMapping("onsaveDirs")
    public String onsaveDirs(String fid, HttpSession session, String aa) throws Exception {

            User user = (User) session.getAttribute("user");
            if (fid != null) {
                session.setAttribute("onsaveDirs", fid);
                session.setAttribute("message", "去选择保存路径");
                return "redirect:/mydoc/queryOne";
            } else {
                if (aa.equals("0")) {
                    session.setAttribute("onsaveDirs", null);
                    return "redirect:/mydoc/queryOne";
                } else if (aa.equals("1")) {
                    fid = (String) session.getAttribute("onsaveDirs");
                    Share share = shareService.queryById2(fid);
                    User userold = userService.selectUserById(share.getFromId());
                    //要移动的文件夹
                    Dir dir1 = dirService.selectById(share.getDir().getId());
                    String folderid = (String) session.getAttribute("did");
                    //目标文件夹
                    Dir dir = dirService.selectById(folderid);
                    //保存文件


                    if (dir.getId().equals(dir1.getDid())) {
                        //有相同目录不移动
                        session.setAttribute("message", "已经复制过了");
                        session.setAttribute("onsaveDirs", null);
                        return "redirect:/mydoc/queryOne";
                    }
                    int status = 1;
                    String patha = dir.getPath() + "\\" + dir.getName();
                    Dir dirSearch = dirService.selectByNamePid(user, status, patha, dir1.getName());
                    if (dirSearch != null) {
                        //同一目录不移动
                        session.setAttribute("message", "已经复制过了");
                        session.setAttribute("onsaveDirs", null);
                        return "redirect:/mydoc/queryOne";
                    }
                    String path = dir.getPath() + "\\" + dir.getName();
                    String path1 = dir1.getPath() + "\\" + dir1.getName();
                    String path0 = path1.replaceAll("\\\\", "\\\\\\\\\\\\\\\\");


                    //要保证这两个方法都能查到pid,uid
                    //2.查询所有

                    List<File> fileList = fileService.selectAllFileByFatherFolder(path0, userold, status);

                    List<Dir> dirList = dirService.selectAllFolderBy(path0, userold, status);

                    dir1.setDid(dir.getId());
                    dirList.add(dir1);
                    String dir0Id = dir1.getId() + "1";
                    String dir0Pid = dir1.getDid();

                    //数据库操作
                    //5.数据库复制信息
                    String pathFirst;

                    for (File file : fileList) {
                        pathFirst = dir.getPath() + "\\" + dir.getName();
                        String[] ff = file.getPath().split("\\\\");


                        boolean judge = false;
                        for (int i = 0; i < ff.length; i++) {
                            //要移动文件夹的名字等于文件路径上的名字
                            if (ff[i].equals(dir1.getName())) {
                                //进入到judge判断
                                judge = true;
                            }
                            if (judge) {
                                pathFirst += "\\" + ff[i];
                            }
                        }
                        //新路径pathFirst
                        //

                        //处理完成，
                        //进入处理数据库环节
                        //更新数据
                        file.setId(file.getId() + "1");
                        file.setUser(user);
                        file.setPath(pathFirst);
                        fileService.addFile(file);
                        //更新文件成功
                        //一起添加的话，他的pid就找不到了
                        String filePid = file.getDir().getId() + "1";
                        String fileId = file.getId();
                        fileService.updateDid(fileId, filePid);
                    }

                    for (Dir dir2 : dirList) {
                        pathFirst = dir.getPath() + "\\" + dir.getName();
                        String[] ff = dir2.getPath().split("\\\\");
                        boolean judge = false;
                        for (int i = 0; i < ff.length; i++) {
                            if (ff[i].equals(dir1.getName())) {
                                judge = true;
                            }
                            if (judge) {
                                pathFirst += "\\" + ff[i];
                            }
                        }
                        //新路径pathFirst

                        //处理完成，
                        //进入处理数据库环节
                        //更新数据
                        dir2.setPath(pathFirst);
                        dir2.setId(dir2.getId() + "1");
                        dir2.setUser(user);
                        dir2.setDid(dir2.getDid() + "1");

                        //Dir dir3 = dir2;对象是引用数据类型的

                        //新的的目录2
                        dirService.addRootDir(dir2);

                    }
                    //最后还原一下原来要移动文件的pid
                    dirService.updatePid(dir0Id, dir0Pid);
                    //1.服务器移动
                    TextCopyFileAndMove.copyDir(path, path1);
                    //服务器移动完成

                    //保存成功
                    session.setAttribute("onsaveDirs", null);
                    session.setAttribute("message", "保存成功");
                    return "redirect:/mydoc/queryOne";
                }
            }
            return "redirect:/mydoc/queryOne";

    }

    @RequestMapping("inSearch")
    public String inSearch(String inSearchName, HttpSession session) {

        List<Share> shareListfiles = shareService.queryAllFilesLike(inSearchName);

            for (Share share : shareListfiles) {
                if (share.getFile() == null) {
                    shareService.deleteById(share.getId());
                }
            }
            shareListfiles = shareService.queryAllFilesLike(inSearchName);

            List<Share> shareListDirs = shareService.queryAllDirsLikes(inSearchName);
            for (Share share : shareListDirs) {
                if (share.getDir() == null) {
                    shareService.deleteById(share.getId());
                }
            }
            shareListDirs = shareService.queryAllDirsLikes(inSearchName);
            session.setAttribute("shareListfiles", shareListfiles);
            session.setAttribute("shareListDirs", shareListDirs);
            return "redirect:/jsp/front/main/detail/public.jsp";

    }

}
